package com.cqeec.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.bootdemo.common.R;
import com.cqeec.bootdemo.dto.SetmealDto;
import com.cqeec.bootdemo.entity.Category;
import com.cqeec.bootdemo.entity.Setmeal;
import com.cqeec.bootdemo.service.CategoryService;
import com.cqeec.bootdemo.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setMealService;
    @Autowired
    private CategoryService categoryService;
//分页展示
    @GetMapping("/page")
    public R<Page<SetmealDto>> getPage(int page, int pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        Page<Setmeal> pageSetmeal = setMealService.page(page1, wrapper);
        Page<SetmealDto> page2 = new Page<>();
        BeanUtils.copyProperties(pageSetmeal, page2, "records");
        List<Setmeal> records = pageSetmeal.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(list);
        return R.success(page2);
    }
    //套餐客服端展示
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.info("查询的套餐信息为：{}", setmeal);
        //根据分类ID查询，正在售卖的套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!= null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setMealService.list(queryWrapper);
        return R.success(list);
    }
    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("新增套餐:{}", setmealDto);
        setMealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto setmealDto = setMealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        List<Setmeal> list=new ArrayList<>();
        for (Long id:ids){
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            list.add(setmeal);
        }
        setMealService.updateBatchById(list);
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setMealService.removeWithValidation(ids);
        return R.success("套餐删除成功");
    }


    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setMealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

}
